//
//  ProductViewController.m
//  freeLance
//
//  Created by Igor Karpenko on 3/27/13.
//  Copyright (c) 2013 Mozi Development. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "UIWebViewController.h"

#import "ProductViewController.h"


@interface ProductViewController ()

@end

@implementation ProductViewController


#pragma mark - Interface methods

-(void) viewDidLoad {
    [super viewDidLoad];

	_dataSource = [[ProductDataSource alloc] initWithProduct:_product];
	
	_photoViewController = [[PhotoViewController alloc] initWithDataSource:_dataSource andStartWithPhotoAtIndex:0];
	_photoViewController.delegate = self;
	self.title = _product.name;

	if (IS_IPAD) {
		UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"prd_background"]];
		[self.view addSubview:background];
		[self.view sendSubviewToBack:background];
        self.view.backgroundColor = [UIColor clearColor];
	} else {
		self.view.backgroundColor = RGB(236.0f, 240.0f, 241.0f);
	}

	_titleLabel.text = _product.name;
	_priceLabel.text = [NSString stringWithFormat:@"%@ р.", _product.price];
	
	UIColor *borderColor = RGB(189.0f, 195.0f, 199.0f);
	_conteiner.layer.borderWidth = 1.0f;
	_conteiner.layer.borderColor = [borderColor CGColor];
	[self.view insertSubview:_photoViewController.view belowSubview:self.openSiteButton];
	_conteiner.clipsToBounds = YES;
    _pageControll.numberOfPages = _product.images.count;
	_pageControll.currentPage = 0;
}


-(void) viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	
	[_photoViewController viewWillAppear:animated];
}


-(void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];

}


#pragma mark - Action methods

-(IBAction) openSiteButtonTapped:(id)sender {
    UIWebViewController *webViewController = [[UIWebViewController alloc] initWithStremUrl:[NSURL URLWithString:self.product.url]];
	[self.navigationController pushViewController:webViewController animated:YES];
}


-(IBAction) closeButtonTapped:(id)sender {
    if ([self.delegate respondsToSelector:@selector(productViewControllerDidColose:)]) {
        [self.delegate productViewControllerDidColose:self];
    }
    
    [UIView animateWithDuration:0.2f animations:^{
        self.view.transform = CGAffineTransformMakeScale(0.3f, 0.3f);
        self.view.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}


#pragma mark - Delegated methods

-(void) photoViewController:(PhotoViewController *)controller selectPhotoAtIndex:(NSInteger)index {
	_pageControll.currentPage = index;
}


@end
